package com.znow.admin.system.security.controller;

import com.znow.admin.system.security.config.SysConfig;
import com.znow.admin.system.security.model.dto.SysUserDetailsDTO;
import com.znow.admin.system.security.model.res.LoginRequest;
import com.znow.admin.system.security.util.SecurityUtils;
import com.znow.base.annotation.AuthRequire;
import com.znow.common.model.CommonResult;
import com.znow.common.utils.jwt.JwtTokenUtils;
import lombok.AllArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录控制器
 *
 * @author Nemo
 * @date 2022/09/30
 */
@RequestMapping("/sys")
@RestController
@AllArgsConstructor
public class LoginController {

    private final AuthenticationManager authenticationManager;

    private final UserDetailsService sysUserService;

    private final SysConfig sysConfig;

    /**
     * 登录
     *
     * @return {@link CommonResult}<{@link SysUserDetailsDTO}>
     */
    @AuthRequire
    @PostMapping("/login")
    public CommonResult<SysUserDetailsDTO> login(@RequestBody LoginRequest loginRequest) {
        String username = loginRequest.getUsername();
        String password = loginRequest.getPassword();
        // 登录接口返回token
        SysUserDetailsDTO userDTO = (SysUserDetailsDTO) sysUserService.loadUserByUsername(username);
        if (userDTO == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        UsernamePasswordAuthenticationToken usernamePasswordToken = new UsernamePasswordAuthenticationToken(username, password);
        Authentication authentication = authenticationManager.authenticate(usernamePasswordToken);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        // 重新生成token
        String token = JwtTokenUtils.generateToken(userDTO, sysConfig.getTokenExpireTime());

        final SysUserDetailsDTO sysUserDetailsDTO = new SysUserDetailsDTO();
        sysUserDetailsDTO.setUsername(username);
        sysUserDetailsDTO.setPassword(null);
        sysUserDetailsDTO.setToken(token);
        return CommonResult.success(sysUserDetailsDTO);
    }

    @PostMapping("/logout")
    public CommonResult<Boolean> logout() {
        return CommonResult.success(true);
    }

    @PostMapping("/getUserInfo")
    public CommonResult<SysUserDetailsDTO> getUserInfo() {
        return CommonResult.success(SecurityUtils.getUser());
    }
}
